package com.example.zqm.git72.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zqm.git72.Adapter.MasterVideoListAdapter;
import com.example.zqm.git72.Fragment.MasterFragment;
import com.example.zqm.git72.JavaBean.Master;
import com.example.zqm.git72.Model.MasterModel;
import com.example.zqm.git72.R;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MasterDetailActivity extends AppCompatActivity {
    String id;
    private TextView mastername;
    private ImageView masterpic;
    private TextView mvideo_name;
    private TextView intro;
    private  TextView place;
    private TextView masterco;
    private Button back;
    private Button problem;
    public Master master;
    public List<Master> masterList;
    public List<Master.MvideoEntity> mvideo;
    RecyclerView recyclerView;
    private ImageButton collect;
    String fan_id;
    String user_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_masterdetail);
        recyclerView=(RecyclerView) findViewById(R.id.mastercourse);
        recyclerView.setLayoutManager(new LinearLayoutManager(MasterDetailActivity.this));
        intNN();
        initView();
        initGG();
//        showData();

    }
    public void initView(){
        mastername =(TextView)findViewById(R.id.mastername);
        masterpic =(ImageView)findViewById(R.id.masterpic);
        intro =(TextView)findViewById(R.id.intro);
        place =(TextView)findViewById(R.id.place);
        masterco =(TextView)findViewById(R.id.masterco);
        problem =(Button)findViewById(R.id.problem);
        collect=(ImageButton)findViewById(R.id.collect);
        back =(Button)findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent();
                intent.setClass(MasterDetailActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
        problem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent();
                intent.setClass(MasterDetailActivity.this,MasterFaqActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });

    }
    protected void intNN(){
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        id=bundle.getString("id");
        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id=sharedPreferences.getString("user_id","");
        MasterModel masterModel =new MasterModel();
        Call<Master> call= masterModel.getMasterById( id);
        call.enqueue(new Callback<Master>() {
            @Override
            public void onResponse(Call<Master> call, Response<Master> response) {
                    master = response.body();
                    mastername.setText(master.getRealname());
                    intro.setText(master.getIntroduction());
                    place.setText(master.getProvince_name()+master.getCity_name()+master.getClub_name());
                String picUrl = "http://172.24.10.175/workout/Uploads/" + master.getThumb();
                Picasso.with(MasterDetailActivity.this).load(picUrl.toString())
                        .resize(200,50).into(masterpic);

                mvideo = master.getMvideo();
               recyclerView.setAdapter(new MasterVideoListAdapter(MasterDetailActivity.this, mvideo));//设置适配器
//                        System.err.println("master course ok");

            }

            @Override
            public void onFailure(Call<Master> call, Throwable t) {

            }
        });

    }
    public void  initGG(){
        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fan_id = id;
                MasterModel masterModel =new MasterModel();
                Call<Integer> call =masterModel.iscollect(fan_id,user_id);
                call.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                        Toast.makeText(MasterDetailActivity.this,"关注成功",Toast.LENGTH_SHORT).show();
                        collect.setImageResource(R.drawable.shoucang);
                    }

                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {

                    }
                });
            }
        });
    }


//    public void showData() {
//
//                MasterModel clubModel = new MasterModel();
//                Call<List<Master>> call = clubModel.getVideo(id);
//                call.enqueue(new Callback<List<Master>>() {
//                    @Override
//                    public void onResponse(Call<List<Master>> call, Response<List<Master>> response) {
//                        masterList = response.body();
//                        recyclerView.setAdapter(new MasterVideoListAdapter(MasterDetailActivity.this, masterList));//设置适配器
//                        System.err.println("master course ok");
//                    }
//
//                    @Override
//                    public void onFailure(Call<List<Master>> call, Throwable t) {
//                        System.err.println("master course filed " + t.getLocalizedMessage());
//
//                    }
//                });
//            }


}
